/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaFormItem } from "@mineadmin/form";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { concat } from "lodash-es";
import MaRemoteSelect from "@/components/ma-remote-select/index.vue";

import { page as commodityPage } from "$/attractions/commerce/api/commerceCommodity.ts";

export default function getFormItems(
  formType: "add" | "edit" = "add",
  t: any,
  model: any
): MaFormItem[] {
  return concat([
    {
      label: "商品名称",
      prop: "commodity_id",
      render: () => MaRemoteSelect,
      renderProps: {
        api: commodityPage,
        dataHandle: (response: any) => {
          if (response.code === 200 && response.data && response.data.list) {
            return response.data.list.map((item: any) => ({
              label: item.commodity_name || `商品名称${item.id}`,
              value: item.id,
              // 将完整的商品信息直接展开到选项中
              id: item.id,
              commodity_name: item.commodity_name,
              // 保留原始数据引用
              raw: item,
            }));
          }
          return [];
        },
        placeholder: "请选择商品",
        clearable: true,
        multiple: false,
        // 监听值变化事件（处理清空操作）
        onChange: (value: any) => {
          if (!value) {
            // 清空时同时清空两个字段
            model.commodity_id = null;
            model.commodity_name = null;
            console.log('通过change事件清空商品选择'); // 调试信息
          }
        },
        // 监听选择项变化事件
        "onSelect-item": (selectedItem: any) => {
          if (selectedItem) {
            // 同时更新商品ID和商品名称
            model.commodity_id = selectedItem.id || selectedItem.value;
            model.commodity_name = selectedItem.commodity_name || selectedItem.label;
            console.log('选中商品:', selectedItem); // 调试信息
          }
        },
      },
    },
    // {
    //   label: "已选商品",
    //   prop: "commodity_name",
    //   render: "input",
    //   renderProps: { 
    //     placeholder: "系统自动填入",
    //     readonly: true,
    //     disabled: true,
    //   },
    // },
    {
      label: "订单id",
      prop: "order_id",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "订单id" }) },
      itemProps: { rules: [{ required: true, message: "请输入订单id" }] },
    },

    {
      label: "数量",
      prop: "quantity",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "数量" }) },
      itemProps: { rules: [{ required: true, message: "请输入数量" }] },
    },
    {
      label: "单价",
      prop: "price",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "单价" }) },
      itemProps: { rules: [{ required: true, message: "请输入单价" }] },
    },
    {
      label: "折扣",
      prop: "discount",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "折扣" }) },
      itemProps: { rules: [{ required: true, message: "请输入折扣" }] },
    },
    {
      label: "总价",
      prop: "total_price",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "总价" }) },
      itemProps: { rules: [{ required: true, message: "请输入总价" }] },
    },
  ]);
}
